var controller = '';
var page = '';
var image_ids = new Array();

$(document).ready(function(){
    controller = $('input[name="controller"]').val();
    page = $('select[name="page_option"]').val();
    
    initialize();
    
    var hidden_id = $('input[name="id"]').val();
    if(hidden_id){
        
        var id = $(this).attr('oid');
        var src = BASE_URL+'callback/pic/'+hidden_id+'/45/';
        var content  = '<img src="'+src+'" class="curb"/>'
        $('td[cin="location"]').append(content);
        
    }
    
});


function initialize(){
    
    var default_img_src = BASE_URL+'imports/images/standard/checkbox.png'; 
    var checked_img_src = BASE_URL+'imports/images/standard/checkbox_checked.png'; 
    
    $(".table-list th").click(function(){
        
        var order = $(this).attr('class');
        var field = $(this).attr('field');
        
        if(!field){return false;}
        
        if(order=='ASC'||order==''){
            order = 'DESC';
        }else{
            order = 'ASC';
        }
        var url = BASE_URL+controller+'/sort/'+field+'/'+order;
        o.preloader({id:1});
        $.post(url, {ajax:true}, function(data){
            $("#current-page").html(data.html);
            o.preloader({id:1,hide:true});
            initialize();
        }, 'json');
        
    });

    $('input[name="cancel"]').click(function(){window.location = BASE_URL+controller;});
    
    if(controller == 'banners' && $('.fancybox').attr('class')){
        
        fancybox_init();
    }
    
    
    $(".checkbox").click(function(){
        var current_src = $(this).attr('src');
        var index = $(this).attr('index');
        if(current_src==default_img_src){
            $(this).attr('src',checked_img_src);
            $('input[value="'+index+'"]').attr('checked',true);
        }else{
            $(this).attr('src',default_img_src);
            $('input[value="'+index+'"]').attr('checked',false);
        }
    });
    
    
    $('input[name="all"]').click(function(){
        var check = this.checked;
        $(".checkbox").attr('src',(check)?checked_img_src:default_img_src);
        $(".boxes").each(function(){
            this.checked = check;
            
        });
    })
    
    $(".delete-from-list").click(function(){
        
        var ids = o.encode(".boxes");
        if(ids.ctr>1){
            var t = confirm(delete_alert);
            if(t){
                var res = o.encode({controller:controller,page:page,action:'delete',ids:ids.data});
                $.post(BASE_URL+'callback',{param:res.data},function(data){call_back.action(data)},'json');
            }
        }
            
    });
    
    $(".delete_this").click(function(){
        var index = $(this).attr('index');
        //alert(index);
        $('img[class="checkbox"][index="'+index+'"]').click();
        $(".delete-from-list").click();
        $('img[class="checkbox"][index="'+index+'"]').click();
    });
    
}

var call_back = {
    action:function(param){
        switch(param.success){
            case 'yes':
                ret = true;
                break;
            default:
                
                break;
        }
        
        if(param.redirect){
            window.location = param.url;
            return false;
        }
        
        
        if(param.message){alert(param.message);}
        return param.result;
    }
};

var o = {
    preloader:function(param){
        var index = ".preloader-"+param.id;
        if(param.hide){
            $(index).hide('slow');
        }else{
            $(index).show();
        }
    },
    enabled:function(param){
        (param.active)?$(param.index).removeAttr('disabled'):$(param.index).attr('disabled', 'disabled');
    },
    ajax_submit:function(param){
        $(param.index).ajaxForm({
            dataType:param.dataType,
            beforeSubmit:param.beforeSubmit,
            success:param.success
        });
    },
    a_submit:function(param){
        $(param.index).ajaxForm({
            'dataType':param.dataType,
            'beforeSubmit':param.beforeSubmit,
            'success':param.success
        });
    },
    encode:function(index){
        var open = '[', close=']', ret = '', ctr = 1;
        
        if(typeof index == 'object'){
            $.each(index, function(i, v){
                if(typeof i == 'string'){
                    close = '}';open = '{';
                    if(typeof v == 'object'){
                        var res = o.encode(v);
                        v = res.data;
                        alert('shh');
                        ret += (ctr>1)?', "'+i+'":'+v+'':'"'+i+'":'+v+'';
                    }else{
                        ret += (ctr>1)?', "'+i+'":"'+v+'"':'"'+i+'":"'+v+'"';
                    }
                    
                }else{
                    ret += (ctr>1)?', '+v:v;
                }
                
                ctr++
            });
            
        }else{
            var type = $(index).attr('type');
            
            if(type=='checkbox'){
                $(index).each(function(){
                    if(this.checked){
                        var v = $(this).val();
                        ret += (ctr>1)?', '+v:v;
                        ctr++
                    }
                });
            }else{
                $(index).each(function(){
                    var v = $(this).val();
                    ret += (ctr>1)?', '+v:v;
                    ctr++
                });
            }
            
                
        }
        
        return {ctr:ctr,data:open+ret+close} ;
    },
    in_array:function(niddle, haystack){
        var ret = false;
        $.each(haystack, function(i, v){
            if(niddle == v){
                ret = true;
                return false;
            }
        });
        return ret;
    }
};


/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


